# -*- coding: iso-8859-1 -*-


"""
Hexagone class - contains everything usefull to manage hexs

Catanyste - Copyright 2008-2009 Julien RAIGNEAU
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
the specific language governing permissions and limitations under the License. 

@author J.Raigneau
"""

import logging
import math
from hexagone import Hexagone,ListHexagone, Thief

from utils import opj
from gamecomponents import GameComponents, ResourceFieldManager
from hexacoordinate import HexaCoordinate

class Board(object):
    
    thief = None
    listeHexagoneTriee = None
    hexacoord = None

    def __init__(self, frameSize):
        """
        Inits Board class
        """
        #TODO: Manage board size + options (number of player)
        self.thief = Thief((99,99))
        self.listeHexagoneTriee = ListHexagone() #hack

        manager = ResourceFieldManager()
        #hack manager while not using port3 and port2
        manager.resource_fields[GameComponents.SEA_FIELDS][GameComponents.WATER] = 18
        manager.resource_fields_ref[GameComponents.SEA_FIELDS][GameComponents.WATER] = 18
        #--end hack
        # constants to improve code readibiliy
        takeRand = getattr(manager,'takeRandomEarthField')
        takeWater_ = getattr(manager,'takeResourceField')(GameComponents.WATER)
        # the hexes
        """
        #alternate shifted columns
        listeHexa = [((3,0),takeWater_),((3,1),takeRand()),((3,2),takeRand()),((3,3),takeRand()),((3,4),takeRand()),((3,5),takeRand()),((3,6),takeWater_),
                     ((2,1),takeWater_),((2,2),takeRand()),((2,3),takeRand()),((2,4),takeRand()),((2,5),takeRand()),((2,6),takeWater_),
                     ((1,1),takeWater_),((1,2),takeRand()),((1,3),takeRand()),((1,4),takeRand()),((1,5),takeWater_),
                     ((0,2),takeWater_),((0,3),takeWater_),((0,4),takeWater_),((0,5),takeWater_),
                     ((4,1),takeWater_),((4,2),takeRand()),((4,3),takeRand()),((4,4),takeRand()),((4,5),takeRand()),((4,6),takeWater_),
                     ((5,1),takeWater_),((5,2),takeRand()),((5,3),takeRand()),((5,4),takeRand()),((5,5),takeWater_),
                     ((6,2),takeWater_),((6,3),takeWater_),((6,4),takeWater_),((6,5),takeWater_)
                     ]
        """
        width, height = frameSize
        #compute hexaCoordinate system
        if width > height :
            orient = HexaCoordinate.UPDOWN
            L = 1.0 * height / 6.5    #5.5 hexes (but take 6.5 for margin) for a 4-player board -- to customize for 5,6 players
        else :
            orient = HexaCoordinate.LEFTRIGHT
            L = 1.0 * width / 6.5     #5.5 hexes (but take 6.5 for margin) for a 4-player board -- to customize for 5,6 players
        l = math.sqrt(3) * L / 2.0    #perfect Hex
        self.hexacoord = HexaCoordinate(orient,(L,l),(width/2,height/2))

        listeHexa = []
        for u in range(-4, 4):
            for v in range (-4, 4):
                if self.hexacoord.distanceD6((u,v)) < 3:
                    listeHexa.append(((u,v), takeRand()))
                elif self.hexacoord.distanceD6((u,v)) == 3:
                    listeHexa.append(((u,v), takeWater_))
                else :
                    pass
                    
        #create set of hexes
        for coord in listeHexa:
            uv,type = coord
            hexa = Hexagone(self,uv,type,99) ##99 is arbitary
            hexa.hexacoord = self.hexacoord   #attach the same HexaCoordinate to the hexes
            self.listeHexagoneTriee.AddHexagone(hexa)
            if type == Hexagone.DESERT:
                self.PutThief(hexa)
        
     
    def PutThief(self, hex):
        """
        Puts thief on hex
        """
        self.thief.SetThief(hex)
         
    def GetThief(self):
        """
        Returns thief coord
        """
        return self.thief.GetCoord()
                